﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI.WebControls;

namespace fanfu.Common
{
    /// <summary>
    /// 枚举的扩展功能类
    /// </summary>
    public sealed class EnumPlus
    {
        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(Enum e)
        {
            FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
            DescriptionAttribute[] EnumAttributes
                = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (EnumAttributes.Length > 0)
            {
                return EnumAttributes[0].Description;
            }
            return e.ToString();
        }
        #endregion

        #region 将含有描述信息的枚举绑定到列表控件中

        /// <summary>
        /// 将含有描述信息的枚举绑定到列表控件中
        /// </summary>
        /// <param name="listControl"></param>
        /// <param name="enumType"></param>
        public static void BindDesEnumToListControl(ListControl listControl, System.Type enumType)
        {
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                listControl.Items.Add(new ListItem(GetEnumDesc(e), ((int)enumValue).ToString()));
            }
        }

        /// <summary>
        /// 重载将含有描述信息的枚举绑定到列表控件中的方法
        /// </summary>
        /// <param name="listControl">控件名字</param>
        /// <param name="enumType">枚举类型申明</param>
        /// <param name="isInt">需要给Value什么值，True的话为常量值数字(int类型)，False为常量值名称(英文string类型)</param>
        public static void BindDesEnumToListControl(ListControl listControl, System.Type enumType, bool isInt)
        {
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                if (isInt)
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), ((int)enumValue).ToString()));
                else
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), enumValue.ToString()));
            }
        }

        /// <summary>
        /// 重载将含有描述信息的枚举绑定到列表控件中的方法
        /// </summary>
        /// <param name="listControl">控件名字</param>
        /// <param name="enumType">枚举类型申明</param>
        /// <param name="isInt">需要给Value什么值，True的话为常量值数字(int类型)，False为常量值名称(英文string类型)</param>
        /// <param name="listItem">自定义ListItem</param>
        public static void BindDesEnumToListControl(ListControl listControl, System.Type enumType, bool isInt, ListItem listItem)
        {
            listControl.Items.Add(listItem);
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                if (isInt)
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), ((int)enumValue).ToString()));
                else
                    listControl.Items.Add(new ListItem(GetEnumDesc(e), enumValue.ToString()));
            }
        }
        #endregion
    }
}
